import { NavigationActions } from 'react-navigation'
import {GOTO_VIEW} from "./constants"

export const naviPush = (routeName, params) => {
    return dispatch => {
        dispatch(NavigationActions.navigate({
              key: routeName,
              
              routeName: routeName,
            
              params: params,
            
              action: NavigationActions.navigate({ routeName: routeName})
        }))
    }
};

export const naviBack = (routeName) => {
    return dispatch => {
        if (routeName == undefined){
            dispatch(NavigationActions.back())
        } else {
            dispatch(NavigationActions.back({
                key: routeName,
                routeName: routeName
            }))
        }
    }
};   

export const naviReset = (routeNames) => {
    return dispatch => {
        let actions = [NavigationActions.navigate({ routeName: 'Home' })];

        if (routeNames != undefined) {
            routeNames.forEach(element => {
                actions.push(NavigationActions.navigate({ routeName: element}));
            });
        }

        const resetAction = NavigationActions.reset({
            index: actions.length - 1,
            actions: actions
        })
        dispatch(resetAction);
    }
};   


export const gotoView = (value) => {
    return dispatch => {
        dispatch({
            type:GOTO_VIEW,
            payload:{
                homeView:value
            }
        })
    }
}



